//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/.
// 

package mysensornetwork.simulations;

import org.mixim.modules.phy.PhyLayerBattery;
import mysensornetwork.src.mac.*;
import mysensornetwork.src.net.*;

//
// This module implements a BMAC network interface card using
// the "BMACLayer" MAC layer module
//
module NicRSMAC
{	
    parameters:
        string connectionManagerName = default("");   //name of the ConnectionManager module
        // power consumption from boards (at 3.3V)
	    double sleepCurrent @unit(mA) = 0.000021mA; // 0.021 µA
		double rxCurrent @unit(mA) = 18.8 mA;
		double decodingCurrentDelta @unit(mA) = 0 mA;
		double txCurrent @unit(mA) = 17.4 mA;
		double setupRxCurrent @unit(mA) = 0.6391mA; // 0.002109 W = (ESetupPower+ESetupXtal+ETxCalibrate)/TSetupTx
		double setupTxCurrent @unit(mA) = 0.6845mA; // 0.002259 W = (ESetupPower+ESetupXtal+ERxCalibrate)/TSetupRx
		double rxTxCurrent @unit(mA) = 18.8 mA; // Upper bound
		double txRxCurrent @unit(mA) = 18.8 mA; // idem
		
		
    gates:
        input upperGateIn; // to upper layers
        output upperGateOut; // from upper layers
        output upperControlOut; // control information 
        input upperControlIn; // control information 
		input radioIn; // radioIn gate for sendDirect

    submodules:
        mac: RealTimeSMACLayer {
            @display("p=96,87;i=block/layer");
        }
        
        phy: PhyLayerBattery {
            parameters:
                decider = xmldoc("Nic802154_TI_CC2420_Decider.xml");
                //publishRSSIAlways = false;
                headerLength = 48 bit; // ieee 802.15.4                
                thermalNoise = -110 dBm;
                // From TI CC1100 datasheet rev. C
                timeSleepToRX = 0.001792 s;
                timeSleepToTX = 0.001792 s;
                timeRXToTX = 0.000192 s;
                timeTXToRX = 0.000192 s;
                timeRXToSleep = 0 s;
                timeTXToSleep = 0 s;
                // power consumption from boards
//                sleepPower = 0.0000000693 W; // 0.021 microA * 3.3 V
//                rxPower = 0.06204 W;   // 18.8 mA * 3.3 V
//                txPower = 0.05742 W;   // 17.4 mA * 3.3 V
//                rxTxPower = 0.06204 W; // Upper bound : 18.8 mA * 3.3 V
//                txRxPower = 0.06204 W; // idem
//                setupTxPower = 0.002109 W; // (ESetupPower+ESetupXtal+ETxCalibrate)/TSetupTx
//                setupRxPower = 0.002259 W; // (ESetupPower+ESetupXtal+ERxCalibrate)/TSetupRx
                @display("p=96,236;i=block/wrxtx");
        }

    connections:
        mac.upperGateOut --> { @display("ls=black;m=m,25,50,25,0"); } --> upperGateOut;
        mac.upperGateIn <-- { @display("ls=black;m=m,15,50,15,0"); } <-- upperGateIn;
        mac.upperControlOut --> { @display("ls=red;m=m,75,50,75,0"); } --> upperControlOut;
       	mac.upperControlIn <-- { @display("ls=red;m=m,85,0,85,0"); } <-- upperControlIn;

        phy.upperGateOut --> { @display("ls=black;m=m,25,50,25,0"); } --> mac.lowerGateIn;
        phy.upperGateIn <-- { @display("ls=black;m=m,15,50,15,0"); } <-- mac.lowerGateOut;
        phy.upperControlOut --> { @display("ls=red;m=m,75,50,75,0"); } --> mac.lowerControlIn;
        phy.upperControlIn <-- { @display("ls=red;m=m,85,0,85,0"); } <-- mac.lowerControlOut;

        radioIn --> phy.radioIn;

}

